<template>
  <div>
    <el-form :model="addData" label-width="100px" size="small">
      <el-form-item label="图标">
        <el-upload style="display:inline-block"
          :auto-upload="false" 
          list-type="picture" 
          action="#" 
          :show-file-list="false" 
          :on-change="imgChange" 
          ref='upload'>
          <el-button type="primary" size="mini" slot="trigger" style="margin-left:15px" @click="imgIndex = 1">
            <span>上传图片</span>
          </el-button>
        </el-upload>
      </el-form-item>
      <el-form-item v-if="addData.filePut !== ''">
        <div>
          <img :src="addData.filePut" style="width: 80px; height: 80px">
        </div>
      </el-form-item>
      <el-form-item label="选中状态图标">
        <el-upload style="display:inline-block"
          :auto-upload="false" 
          list-type="picture" 
          action="#" 
          :show-file-list="false" 
          :on-change="imgChange" 
          ref='upload'>
          <el-button type="primary" size="mini" slot="trigger" style="margin-left:15px" @click="imgIndex = 2">
            <span>上传图片</span>
          </el-button>
        </el-upload>
      </el-form-item>
      <el-form-item v-if="addData.filePutSelection !== ''">
        <div>
          <img :src="addData.filePutSelection" style="width: 80px; height: 80px">
        </div>
      </el-form-item>
      <el-form-item label="名称">
        <el-input v-model="addData.name" placeholder="请输入" style="width: 60%"></el-input>
      </el-form-item>
      <el-form-item label="页面">
        <el-input v-model="addData.choice_page_name_view" placeholder="请输入" style="width: 60%"></el-input>
      </el-form-item>
    </el-form>

    <!-- 提交 -->
    <div style="text-align:center;margin-top:25px">
      <el-button type="primary" style="padding:8px 20px" @click="submit">保 存</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'blendent-add',
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    addData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      imgIndex: 1
    }
  },
  methods: {
    imgChange(file) {
      const reader = new FileReader();
      reader.readAsDataURL(file.raw);
      reader.onload = () => {
        if(this.imgIndex === 1) {
          this.addData.filePut = reader.result;
        }else{
          this.addData.filePutSelection = reader.result;
        }        
      }
    },
    submit() {
      this.$emit('success');
    }
  },
}
</script>